package com.itheima.game3plus;

import com.itheima.game3.Student;

public class test {
    public static void main(String[] args) {
        Student[] stu=new Student[3];
        Student st1=new Student(1,"张三",15);
        Student st2=new Student(2,"李四",16);
        Student st3=new Student(3,"王五",17);
        stu[0]=st1;
        stu[1]=st2;
        stu[2]=st3;
        //通过id删除学生信息
        if(getIndex(stu,9)>=0){
            //id存在
            stu[getIndex(stu,9)]=null;
        }else{
            //id不存在
            System.out.println("当前id不存在");
        }
        //通过id让学生年龄+1
        int index=getIndex(stu,2);
        if(index>=0){
            stu[index].setAge(stu[index].getAge()+1);
        }
        else{
            System.out.println("当前id无学生");
        }
        show(stu);
    }
    public static int getIndex(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                if(arr[i].getId()==id){
                    return i;
                }
            }
        }
        return -1;
    }
    public static void show(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                System.out.println(arr[i].getId()+","+arr[i].getName()+","+arr[i].getAge());
            }
        }
    }
}
